/**
 * Improved Cube UI demonstration in JavaFX
 * @author charmgil, pmwalk
 */
package cube;

import javafx.scene.*;
import javafx.stage.Screen.*;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javax.swing.JFileChooser;
import javafx.scene.control.Tooltip;

// Program position on the screen
var posX = 0;
var posY = 0;

// var
var outfit: Rectangle;
var outfit_inline: Rectangle;
var tooltiptext: String;
var tooltip: TooltipNode;
var tooltip_inline: TooltipNode;

// layout init
Layout.switchLayout( Layout.MODE_INIT );

// main shell
Stage {
    title: "Cube Alpha"
    style: StageStyle.TRANSPARENT
    resizable: false
    x: bind posX
    y: bind posY
    width: Layout.SCR_X
    height: Layout.SCR_Y
    scene: Scene {
        fill: null
        width: Layout.SCR_X
        height: Layout.SCR_Y
        content: [ Group {
                    visible: true
                    content: [
                        outfit = Rectangle{
                            x: bind Layout.obOffsetX, y: bind Layout.obOffsetY
                            width: bind Layout.obW, height: bind Layout.obH
                            arcWidth: bind Layout.obArcW, arcHeight: bind Layout.obArcH
                            
                            fill: Color.TRANSPARENT
                            stroke: bind Layout.obOutColor
                            strokeWidth: 12.0

                            // action hookers
                            blocksMouse: true
                            onKeyPressed: onKeyEvent
                            onMouseClicked: onMouseClick    // running
                            onMouseDragged: onMouseDrag     // move
                            onMouseMoved: onMouseHover    // tooltip
                            //onMouseExited: onMouseOut   // tooltip off
                        }
                        outfit_inline = Rectangle{
                            x: bind Layout.obOffsetX, y: bind Layout.obOffsetY
                            width: bind Layout.obW, height: bind Layout.obH
                            arcWidth: bind Layout.obArcW, arcHeight: bind Layout.obArcH

                            fill: Color.TRANSPARENT
                            stroke: bind Layout.obInColor
                            strokeWidth: 3

                            // action hookers
                            blocksMouse: true
                            onKeyPressed: onKeyEvent
                            onMouseClicked: onMouseClick    // running
                            onMouseDragged: onMouseDrag     // move
                            onMouseMoved: onMouseHover    // tooltip
                            //onMouseExited: onMouseOut   // tooltip off
                        }
                    ]
                },//Group
                Cube.cube,
                tooltip = TooltipNode{
                            sourceNode: outfit
                            tooltip: bind tooltiptext
                },
                tooltip_inline = TooltipNode{
                            sourceNode: outfit_inline
                            tooltip: bind tooltiptext
                }
        ]//content
    }//scene
}//stage



// Event Handlers
function onMouseClick(e: MouseEvent): Void {
    
    // control + click will rotate the cube
    if( e.controlDown ){
        if( Cube.ax != 0 )  return;

        if( outfit.x - 10 < e.x and e.x < outfit.x + 10 ){
            Cube.ax = Layout.acc;
        }else if( outfit.x + outfit.width - 10 < e.x and e.x < outfit.x + outfit.width + 10 ){
            Cube.ax = -Layout.acc;
        }

        return;
    }


    // double click to run the loaded targets
    if( Timer.isChecked ){
        Timer.isChecked = false;
        Timer.clickedTimeLine.stop();

        // action!!!
        Cube.runCube();
        
    }else{
        Timer.clickedTimeLine.playFromStart();
    }
}//function onMouseClick



// dragging on the outer rectangle will move the position of the cube
function onMouseDrag(e: MouseEvent): Void {
    posX += e.dragX as Integer; // the world worst ways of casting
    posY += e.dragY as Integer;
}//function onMouseDrag(e: MouseEvent)

function onMouseHover(e: MouseEvent): Void {
    var temp;
    if( outfit.x - 10 < e.x and e.x < outfit.x + 10 ){
        tooltiptext = "left";

        if( Cube.frontface == 1 )   temp = Cube.f5.targetName
        else if( Cube.frontface == 2 )   temp = Cube.f1.targetName
        else if( Cube.frontface == 3 )   temp = Cube.f2.targetName
        else if( Cube.frontface == 4 )   temp = Cube.f6.targetName;

    }else if( outfit.x + outfit.width - 10 < e.x and e.x < outfit.x + outfit.width + 10 ){
        tooltiptext = "right";

        if( Cube.frontface == 1 )   temp = Cube.f2.targetName
        else if( Cube.frontface == 2 )   temp = Cube.f6.targetName
        else if( Cube.frontface == 3 )   temp = Cube.f5.targetName
        else if( Cube.frontface == 4 )   temp = Cube.f1.targetName;

    }else{
        tooltiptext = "";
        return;
    }

    if( temp == null )    temp = "None";

    tooltiptext = "{tooltiptext}: {temp}";
    //tooltip.activate();

    //println( "{e.x}, {e.y}" );
    
}//function onMouseHover(e: MouseEvent)

function onMouseOut(e: MouseEvent): Void {
    //tooltip.deactivate();

}//function onMouseOut(e: MouseEvent)


// defines several keyboard shortcuts
function onKeyEvent( e: KeyEvent ): Void {
    //print( " fx " );
    //println( "T-char: {e.char} / code: {e.code}" );

    if( e.altDown and e.controlDown ){
        if( e.code == KeyCode.VK_LEFT ){
            Cube.ax = Layout.acc;
        }else if( e.code == KeyCode.VK_RIGHT ){
            Cube.ax = -Layout.acc;
        }else if( e.code == KeyCode.VK_S ){
            println( "save .cube" );
            var fc: JFileChooser;

            if( Cube.f1.target.length() + Cube.f2.target.length() + Cube.f5.target.length() + Cube.f6.target.length() == 0 ){
                println( "nothing to write" );
                return;
            }
            var targets = "{Cube.f1.target}\n{Cube.f2.target}\n{Cube.f6.target}\n{Cube.f5.target}";

            // write
            fc = Utils.getFileChooser( false );
            fc.setSelectedFile( new java.io.File( "Untitle.cube" ) );
            if( JFileChooser.APPROVE_OPTION == fc.showSaveDialog( null ) ){
                println( "?" );

                var file = FileIO{};
                file.writeData( fc.getSelectedFile(), targets );
            }
            return;

        }else if( e.code == KeyCode.VK_O ){
            println( "open .cube" );
            var fc: JFileChooser;

            fc = Utils.getFileChooser( true );
            if( javax.swing.JFileChooser.APPROVE_OPTION == fc.showOpenDialog( null ) ){
                var file = FileIO{};
                var targets = file.readData( fc.getSelectedFile() );

                Cube.f1.target = targets[ 0 ];
                Cube.f2.target = targets[ 1 ];
                Cube.f5.target = targets[ 3 ];
                Cube.f6.target = targets[ 2 ];

                for( i in [ 0 .. 3 ] ){
                    var s = targets[ i ].lastIndexOf( "/" );
                    var t = targets[ i ].lastIndexOf( "." );

                    if( s == -1 )    s = 0;

                    var targetName = if( t == -1 ) targets[ i ].subSequence( s + 1, targets[ i ].length() ) else targets[ i ].subSequence( s + 1, t );
                    var targetType = if( t == -1 ) "UNKNOWN" else targets[ i ].toUpperCase().subSequence( t + 1, targets[ i ].length() );


                    if( i == 0 ){
                        Cube.f1.targetName = targetName;
                        Cube.f1.targetType = targetType;

                        if( targetName != null ){
                            Cube.f1.targetLabelDisplay = true;
                            Cube.f1.targetLabel.showTip.playFromStart();
                            println( "1: {targetName} [{targetType}]" );
                        }
                    }else if( i == 1 ){
                        Cube.f2.targetName = targetName;
                        Cube.f2.targetType = targetType;

                        if( targetName != null ){
                            Cube.f2.targetLabelDisplay = true;
                            Cube.f2.targetLabel.showTip.playFromStart();
                            println( "2: {targetName} [{targetType}]" );
                        }
                    }else if( i == 2 ){
                        Cube.f6.targetName = targetName;
                        Cube.f6.targetType = targetType;

                        if( targetName != null ){
                            Cube.f6.targetLabelDisplay = true;
                            Cube.f6.targetLabel.showTip.playFromStart();
                            println( "3: {targetName} [{targetType}]" );
                        }
                    }else if( i == 3 ){
                        Cube.f5.targetName = targetName;
                        Cube.f5.targetType = targetType;

                        if( targetName != null ){
                            Cube.f5.targetLabelDisplay = true;
                            Cube.f5.targetLabel.showTip.playFromStart();
                            println( "4: {targetName} [{targetType}]" );
                        }
                    }
                }
                

                //
                // target label update
                //
                //
            }

            return;

        }else if( e.code == KeyCode.VK_N ){
            println( "new cube" );
        }
    }
}//function onKeyEvent( e: KeyEvent )



// main routine - will run when the program is loaded
Cube.rotation.play();   // enable rotation
outfit.requestFocus();

